# 导入
import pymysql

# 1.创建连接对象
conn = pymysql.connect(host='localhost', user='root', password='root', database='advanced', port=3306, charset='utf8')

# 2.获取游标对象
cursor = conn.cursor()

# 3.执行SQL语句
sql = 'select * from student'
# 返回受影响的行数
rows = cursor.execute(sql)
print(f'受影响的行数是:{rows}')

# 4.获取数据
# 注意:游标对象中的结果集数据,只能获取一次,获取过后,就不能再获取
# 4.1 获取一行数据
one = cursor.fetchone()
print(one)
# 4.2 获取指定行数,参数是指定获取的记录条数
many = cursor.fetchmany(2)
print(many)
# 4.3 获取所有的结果集
all = cursor.fetchall()
print(all)

# 游标位置设置
cursor.rownumber = 2
many1 = cursor.fetchmany(3)
print(many1)

# 6.关闭游标
cursor.close()

# 7.关闭连接
conn.close()